//
//  HLLRCTableView.m
//  HLMusicBox
//
//  Created by hulinhu on 2018/1/3.
//  Copyright © 2018年 hulinhu. All rights reserved.
//

#import "HLLRCTableView.h"
#import "HLLRCTableCell.h"

@implementation HLLRCTableView {
    
    NSDictionary *mLRCDict;
    HLSongPlayManager *songPlayManager;
    NSInteger lrcIndex;
    HLLRCTableCell  *newLRCCell;
    HLLRCTableCell *oldLRCCell;
}

-(instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        songPlayManager = [HLSongPlayManager shareInstance];
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator = NO;
        // 锁屏
        if (!_lockScreenTableView) {
            _lockScreenTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0 , KScreenWidth,KScreenWidth) style:UITableViewStylePlain];
            _lockScreenTableView.dataSource = self;
            _lockScreenTableView.delegate = self;
            _lockScreenTableView.separatorStyle = NO;
            _lockScreenTableView.backgroundColor = [UIColor clearColor];
            [_lockScreenTableView registerClass:[HLLRCTableCell class] forCellReuseIdentifier:@"HLLRCTableCell"];
        }
    }
    return self;
}

#pragma mark
#pragma mark - 设置滚动歌词

-(void)setLRCScrollToIndex:(NSInteger)lrc_index {
    // 越界 或者 等于直接 返回
    if (lrcIndex == lrc_index || lrc_index > songPlayManager.mLRCDictinary.count-1) {
        return;
    }
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lrc_index inSection:0];
    // 滚动到指定坐标
    [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:lrc_index inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    
    // 获取当前 新Cell 设置颜色
    UITableViewCell *currentCell = [self cellForRowAtIndexPath:indexPath];
    newLRCCell = (HLLRCTableCell *)currentCell;
    [newLRCCell.textLabel setTextColor:[UIColor redColor]];
    
    // 获取之前 旧Cell 设置颜色
    NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:lrcIndex inSection:0];
    UITableViewCell *oldCell = [self cellForRowAtIndexPath:oldIndexPath];
    oldLRCCell = (HLLRCTableCell *)oldCell;
    [oldLRCCell.textLabel setTextColor:[UIColor whiteColor]];
    
    // 设置模式
    [newLRCCell setLRCAnimation:scaleAlways];
    
    // 锁屏歌词 设置
    [_lockScreenTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:lrc_index inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    
    // 锁屏歌词 新Cell 设置颜色
    UITableViewCell *lockCurrentCell = [_lockScreenTableView cellForRowAtIndexPath:indexPath];
    newLRCCell = (HLLRCTableCell *)lockCurrentCell;
    [newLRCCell.textLabel setTextColor:[UIColor redColor]];
    
    
    // 锁屏歌词 旧Cell 设置颜色
    UITableViewCell *lockOldCell = [_lockScreenTableView cellForRowAtIndexPath:oldIndexPath];
    oldLRCCell = (HLLRCTableCell *)lockOldCell;
    [oldLRCCell.textLabel setTextColor:[UIColor whiteColor]];
    
    // 更新歌词下标
    lrcIndex = lrc_index;
}

#pragma - mark tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return mLRCDict.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    static NSString *reuseIdentifier = @"HLLRCTableCell";
    HLLRCTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell == nil) {
        cell =  [[HLLRCTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    cell.textLabel.text = songPlayManager.mLRCDictinary[songPlayManager.mTimeArray[indexPath.row]];
    [cell.textLabel setFont:[UIFont systemFontOfSize:16.0]];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    [cell setBackgroundColor:[UIColor clearColor]];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (_currentRow == indexPath.row) {
        cell.textLabel.textColor = [UIColor redColor];
    }else{
        cell.textLabel.textColor = [UIColor whiteColor];
    }
    
    return cell;
}


#pragma mark
#pragma mark - load LRC
-(void)handleLRCData:(NSString *)lrcString {
    mLRCDict = songPlayManager.mLRCDictinary;
    if (mLRCDict.count == 1) {
        [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
    }
    [self reloadData];

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    _isDragging = YES;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    _isDragging = NO;
}



@end
